package com.frxuexi.park2210.repair

import androidx.lifecycle.MutableLiveData
import com.frxuexi.lib_base.db.CommentDao
import com.frxuexi.lib_base.entity.RepairCommentBean
import com.frxuexi.lib_base.mvvm.BaseViewModel
import com.frxuexi.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject

@HiltViewModel
class RepairViewModel @Inject constructor(val apiService: ApiService,val commentDao: CommentDao):BaseViewModel() {

    var allRepairSuc = MutableLiveData<String>()
//    var selAppointSuc = MutableLiveData<String>()
    var selAppointSuc = MutableStateFlow<String>("")

    var addRepairSuc=MutableStateFlow<String>("")

    var addComentSuc=MutableLiveData<Long>()

    var getCommentsSuc=MutableLiveData<List<RepairCommentBean>>()

    var deleteCommentsSuc=MutableLiveData<Int>()

    var updateCommentsSuc=MutableLiveData<Int>()

    fun getAllRepairs(){
        httpRequest({apiService.getAllRepairs()},allRepairSuc::postValue)
    }

    fun selAppoint(map: Map<String,Any>){
        httpRequest({apiService.selAppointRepair(mapToBody(map))}, { selAppointSuc.value=it/*+map.get("repairId") */})
    }

    fun addRepair(map: Map<String, Any>){
        httpRequest({apiService.addRepair(mapToBody(map))},{addRepairSuc.value=it})
    }

    fun addComment(repairCommentBean: RepairCommentBean){
        roomRequest({commentDao.insert(repairCommentBean)},addComentSuc::postValue)
    }

    fun getComments(repairId:Int){
        roomRequest({commentDao.getComments(repairId)},getCommentsSuc::postValue)
    }

    fun deleteComment(repairCommentBean: RepairCommentBean){
        roomRequest({commentDao.deleteComment(repairCommentBean)},deleteCommentsSuc::postValue)
    }

    fun updateComment(repairCommentBean: RepairCommentBean){
        roomRequest({commentDao.updateComment(repairCommentBean)},updateCommentsSuc::postValue)
    }
}